package com.kaguya.command;

/**
 * 行为型 命令模式
 * 解决：把命令抽象化，易于扩展，在某些场合，例如要对行为进行扩展，例如会议的记录 撤销 重做等
 * 优点：降低系统耦合，易于扩展命令
 * 缺点：类数量爆炸
 */
public class Test {
    public static void main(String[] args) {
        // 实例化命令（开关空调）
        Command onCommand = new TurnOnCommand(new AirConditionReceiver());
        // 实例化处理方（小度智能语音）
        Invoker invoker = new Invoker(onCommand);
        // 执行实际的命令
        invoker.action();
    }
}

/**
 * 发送命令方
 */
class AirConditionReceiver {
    public void turnOn() {
        System.out.println("open air-condition");
    }

    public void turnOff() {
        System.out.println("off air-condition");
    }
}

/**
 * 命令的抽象
 */
interface Command {
    void exec();
}

/**
 * 命令的实现1
 */
class TurnOnCommand implements Command {

    AirConditionReceiver airConditionReceiver;

    public TurnOnCommand(AirConditionReceiver airConditionReceiver) {
        super();
        this.airConditionReceiver = airConditionReceiver;
    }

    @Override
    public void exec() {
        airConditionReceiver.turnOn();
    }
}

/**
 * 命令的实现2
 */
class TurnOffCommand implements Command {
    AirConditionReceiver airConditionReceiver;

    public TurnOffCommand(AirConditionReceiver airConditionReceiver) {
        super();
        this.airConditionReceiver = airConditionReceiver;
    }

    @Override
    public void exec() {

    }
}

/**
 * 处理方
 */
class Invoker {

    private Command command;

    public Invoker(Command command) {
        this.command = command;
    }

    public void action() {
        System.out.println("小度正在为您服务。。。");
        command.exec();
    }
}